import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd{

    //输入正确的账号，登陆成功
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username,String password,String blog_list_url){
        //打开博客登陆页面
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //输入账号admin
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //输入密码123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //跳转到列表页后，获取当前页面的url,如果相同测试通过，如果不同则不通过
        String cur_url=webDriver.getCurrentUrl();
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //列表页展示用户信息是admin,用户名相同测试通过，不同则不通过
        String cur_admin=webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
        Assertions.assertEquals(username,cur_admin);
    }


    //列表博客数量不能为零
    @Order(2)
    @Test
    void BlogList(){
        //打开博客列表页
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取页面所有博客标题对应的元素
        int title_num=webDriver.findElements(By.cssSelector(".title")).size();
        //如果元素数量不为0，测试通过
        Assertions.assertNotEquals(0,title_num);
    }


    /*
     * 博客详情页效验：
     * 1、url
     * 2、博客标题
     * 3、页面title是博客详情页
     * */
    public static Stream<Arguments> Generator() {
        return Stream.of(
                Arguments.arguments("http://42.192.83.143:8563/blog_system/blog_detail.html?",
                "博客详情页","自动化测试"
                ));
    }

    @Order(4)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url,String excepted_title,String excepted_blog_title){
        //找到第一篇博客对应的查看全文的按钮
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("/html/body/div[2]/div[2]/div[1]/a")).click();

        //获取当前页面url
        String cur_url=webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //获取当前页面title
        String cur_title=webDriver.getTitle();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //获取当前博客标题
        String cur_blog_title=webDriver.findElement(By.cssSelector("body > div.container > div.right > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        Assertions.assertEquals(excepted_title,cur_title);
        Assertions.assertEquals(excepted_blog_title,cur_blog_title);
        if(cur_url.contains(excepted_blog_title)){
            System.out.println("测试通过");
        }else{
            System.out.println(cur_url);
            System.out.println("测试不通过");
        }
    }


    //写博客
    @Order(3)
    @Test
    void EditBlog() throws InterruptedException {
        //找到写博客按钮，点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //找到输入框，输入标题
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        sleep(3000);

        //点击发布
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);

        //获取当前页面的url
        String cur_url=webDriver.getCurrentUrl();
        Assertions.assertEquals("http://42.192.83.143:8563/blog_system/blog_list.html",cur_url);
    }


    //效验已发布博客标题
    //效验已发布博客时间
    @Order(5)
    @Test
    void BlogInfoChecked(){
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        //获取第一篇博客标题
        String first_blog_title=webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
        //获取第一篇博客发布时间
        String first_blog_time=webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
        //效验博客标题是不是自动化测试
        Assertions.assertEquals("自动化测试",first_blog_title);
        //如果时间是2023-5-23年发布的则测试通过
        if(first_blog_time.contains("2023-05-23")){
            System.out.println("测试通过！");
        }else{
            System.out.println("当前时间是"+first_blog_time);
            System.out.println("测试不通过！！！");
        }
    }


    //删除
    @Order(6)
    @Test
    void DeleteBlog() throws InterruptedException {
        //打开博客列表页面
        webDriver.get("http://42.192.83.143:8563/blog_system/blog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //点击全文按钮
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //点击删除按钮
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).click();
        sleep(3000);

        //博客列表页第一篇博客标题不是自动化测试
        String first_blog_title=webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]")).getText();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //效验当前博客标题不等于“自动化测试”
        Assertions.assertNotEquals(first_blog_title,"自动化测试");
    }


    //注销
    @Order(7)
    @Test
    void Logout(){
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //效验登录url
        String cur_url=webDriver.getCurrentUrl();
        Assertions.assertEquals("http://42.192.83.143:8563/blog_system/blog_login.html",cur_url);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //效验提交按钮
        WebElement webElement=webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);
    }

}





